{
  //泛型函数
  //声明函数    T 为泛型参数 T  P  V A B C  a b c 均可
  function createArray<T>(count: number, value: T): T[] {
    //声明数组
    let arr: T[] = [];
    //循环
    for (let i = 0; i < count; i++) {
      arr.push(value);
    }
    //返回结果
    return arr;
  }

  //调用
  // let arr = createArray<string>(3, 'love');
  //运行
  // arr[0].toFixed(2);
  // arr[0].toFixed(2);
}

{
  //多个泛型参数的演示   []
  function createArray<T, P>(one: T, two: P): [T, P] {
    return [one, two];
  }

  let res = createArray<string, number>("123", 456);

  // res[1].
  // axios.get   .post  .put  .patch  .delete  都是泛型函数
}

{
  interface Book {
    id: number;
    title: string;
    price: number;
  }

  interface User {
    id: number;
    name: string;
    age: number;
  }

  interface Response<T> {
    status: number;
    headers: object;
    data: T;
  }

  //泛型接口
  let response: Response<User> = {
    status: 200,
    headers: {
      a: 100,
      b: 200,
    },
    data: {
      id: 2,
      name: "张三",
      age: 18,
    },
  };

  let response2: Response<Book> = {
    status: 200,
    headers: {},
    data: {
      id: 2,
      title: "红楼梦",
      price: 20,
    },
  };

  let arr: Array<string> = ["a", "b", "c"];
}

{
  interface User{
    id: number;
    name: string;
    age: number;
  }

  interface Book{
    id: number;
    title: string;
    price: number;
  }

  interface ResponseBody<T>{
    code: string;
    msg: string;
    data: T
  }

  interface Response<T>{
    status: number;
    headers: object;
    data: T;
  }

  //泛型接口
  let response: Response<ResponseBody<User>> = {
    status: 200,
    headers: {
      a: 100,
      b: 200,
    },
    data: {
      code: "000",
      msg: "ok",
      data: {
        id: 2,
        name: "张三",
        age: 18,
      },
    },
  };

  let response2: Response<ResponseBody<Book>> = {
    status: 200,
    headers: {
      a: 100,
      b: 200,
    },
    data: {
      code: "000",
      msg: "ok",
      data: {
        id: 2,
        title: "三国演义",
        price: 18.8
      },
    },
  }

}


{
  //泛型类
  class Container<T>{
    //声明一个属性
    private store: T[];
    //构造方法
    constructor(arr: T[]){
      this.store = arr;
    }
    push(v: T){
      this.store.push(v);
    }
  }

  interface Book{
    id: number;
    title: string;
    price: number;
  }

  //实例化
  // let container = new Container<string>(['西游记','红楼梦','三国演义','水浒传']);
  // container.push('三体');

  let container = new Container<Book>([]);
  
  container.push({
    id: 1,
    title: '西游记',
    price: 15.8
  })

  container.push({
    id: 2,
    title: '红楼梦',
    price: 15.9
  })

  console.log(container);

}

{

  interface Length{
    length: number;
  }

  function fn<T extends Length>(arg: T){
    console.log(arg.length);
  }

  fn('abc');
  fn([1,2,3]);
  // fn(123);
}